import java.awt.Color;
import java.awt.Point;

// this gets the shape from the canvas that a user is drawing 
// blocks until the mouse has been released and we get the final shape.

public class PLCanvasShape extends PLEventQueueAntiFreeze implements PLEndPointsReceiver {
	PLShape mShape = null;
	Color mColor = null;

	public PLCanvasShape(Color color) {mColor = color;}
	
	public PLShape GetShape() {
		PLMouseManager mouse = new PLMouseManager(this);
		mouse.StartListening(PLGuiInfo.Obj().GetCurCanvas());

		mShape = null;

		Loop();
		
		mouse.StopListening(PLGuiInfo.Obj().GetCurCanvas());		

		mouse.Shutdown();
		PLDebug.Warn("Returning shape");
		return mShape;
	}

	public void NotifyIntermediate(Point begin,Point end){
		UpdateShape(begin, end);

		// draw this temp shape to canvas
		PLGuiInfo.Obj().GetCurCanvas().DrawShape(mShape, mColor, true);
	}

	public void NotifyFinal(Point begin,Point end) {
		UpdateShape(begin, end);

		// stop!
		Stop();
		PLDebug.Warn("Stopping");
	}

	private void UpdateShape(Point begin,Point end) {
		if (mShape == null) {
			mShape = PLShapeUtils.MakeNewShape(begin, end);
		} else {	
			mShape.SetStart(begin);
			mShape.SetEnd(end);
		}
	}		
}
